import React from "react";
import {Routes, Route, Outlet} from 'react-router-dom';
import AuthRouter from "./auth";

// 引入首页的组件页面
import Home from "@/components/Home";

// 引入每个目录下面的index.js文件
const ModulesFile = require.context('./',true,/index.js$/);

// 总路由集合
const RouterMap = [];

// 对引入的文件进行循环
ModulesFile.keys().reduce((modules, modulePath) => {
    // 获取路由模块名称
    const ModuleName = modulePath.replace(/^.\/(.*)\.js/,'$1');

    if(ModuleName !== 'index')
    {
        // 路由列表
        const ModuleList = ModulesFile(modulePath)
        
        //追加到数组
        RouterMap.push(...ModuleList.default)
    
    }

    return RouterMap;

},{});

const RouterList = () => {
    return (
        <Routes>
            <Route path="/" element={<Home />} />

            <Route path="/" element={<Outlet />}>
                {RouterMap.map((item,index) => {
                    return (
                        // 把父级路由循环出来
                        <Route key={index} path={item.path}>
                            {item.children && item.children.map((son,idx) => {
                                return (
                                    <Route key={idx} path={son.path} element={<AuthRouter auth={son.auth} component={<son.component />} />} />
                                )
                            })}
                        </Route>
                    )
                })}
            </Route>
        </Routes>
    )
}

export default RouterList;